package com.ruoyi.warehouse.service.impl;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import com.ruoyi.warehouse.domain.CpkcFnishedProductInventory;
import com.ruoyi.warehouse.domain.CprkProductStorageForm;
import com.ruoyi.warehouse.mapper.ProductStorageMapper;
import com.ruoyi.warehouse.service.ProductStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 成品入库Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-03
 */
@Service
public class ProductStorageServiceImpl implements ProductStorageService {

    @Autowired
    private ProductStorageMapper productStorageMapper;
    @Resource
    private CodeClient codeClient;
    /**
     * 查询产品入库
     *
     * @param rkCode 产品入库主键
     * @return 产品入库
     */
    @Transactional
    @Override
    public CprkProductStorageForm selectCprkProductStorageFormByRkCode(String rkCode)
    {
        return productStorageMapper.selectCprkProductStorageFormByRkCode(rkCode);
    }

    /**
     * 查询产品入库列表
     *
     * @param cprkProductStorageForm 产品入库
     * @return 产品入库
     */
    @Transactional
    @Override
    public List<CprkProductStorageForm> selectCprkProductStorageFormList(CprkProductStorageForm cprkProductStorageForm)
    {
        return productStorageMapper.selectCprkProductStorageFormList(cprkProductStorageForm);
    }

    /**
     * 新增产品入库
     *
     * @param cprkProductStorageForm 产品入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCprkProductStorageForm(CprkProductStorageForm cprkProductStorageForm)
    {

        String code = codeClient.getCode(25);
        //编码和记录人赋值
        cprkProductStorageForm.setRkCode(code);
        cprkProductStorageForm.setRkMan(SecurityUtils.getUsername());
        return productStorageMapper.insertCprkProductStorageForm(cprkProductStorageForm);
    }

    /**
     * 修改产品入库
     *
     * @param cprkProductStorageForm 产品入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCprkProductStorageForm(CprkProductStorageForm cprkProductStorageForm)
    {
        return productStorageMapper.updateCprkProductStorageForm(cprkProductStorageForm);
    }

    /**
     * 批量删除产品入库
     *
     * @param rkCodes 需要删除的产品入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCprkProductStorageFormByRkCodes(String[] rkCodes)
    {
        return productStorageMapper.deleteCprkProductStorageFormByRkCodes(rkCodes);
    }

    /**
     * 删除产品入库信息
     *
     * @param rkCode 产品入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCprkProductStorageFormByRkCode(String rkCode)
    {
        return productStorageMapper.deleteCprkProductStorageFormByRkCode(rkCode);
    }

    /**
     * 产品信息
     *
     * @param cpkc 需要删除的数据主键集合
     * @return 结果
     */
    public List<CprkProductStorageForm> selectCp(CpkcFnishedProductInventory cpkc){
        return productStorageMapper.selectCp(cpkc);
    }

    /**
     * 审核
     *
     * @param rkCode 需要删除的数据主键集合
     * @return 结果
     */
    @Transactional
    public int insertCpkcStorage(String rkCode){


        CprkProductStorageForm cprkProductStorageForm = productStorageMapper.selectCprkProductStorageFormByRkCode(rkCode);
        int i = productStorageMapper.selectCpkcCount(cprkProductStorageForm);
        productStorageMapper.updateCprkIsUse(rkCode);
        if(i>0){
            return productStorageMapper.updateCpkcStorage(cprkProductStorageForm);
        }
        return productStorageMapper.insertCpkcStorage(cprkProductStorageForm);
    }

    /**
     * 撤销
     *
     * @param rkCode 需要撤销的数据主键
     * @return 结果
     */
    @Transactional
    public int cancelCpkcStorage(String rkCode){
        // 获取入库信息
        CprkProductStorageForm cprkProductStorageForm = productStorageMapper.selectCprkProductStorageFormByRkCode(rkCode);

        int i = productStorageMapper.updateCpkcStorageCancel(cprkProductStorageForm);
        if(i==0){
            throw new RuntimeException();
        }
        productStorageMapper.updateCprkIsUseCancel(rkCode);
        return i;
    }
}
